//
//  DGGCCycleAdView.m
//  DGGCustomer
//
//  Created by liyanbo on 2019/12/18.
//  Copyright © 2019 dgg_lyb. All rights reserved.
//

#import "DGGCCycleAdView.h"
@interface DGGCCycleAdView ()<SDCycleScrollViewDelegate>

//点击回调
@property (nonatomic, copy) void(^adsClick)(NSInteger index);
//滚动回调
@property (nonatomic, copy) void(^scrollBlock)(NSInteger index);


@end
@implementation DGGCCycleAdView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
            
        [self.cycleView addSubview:self.pageControl];
        [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.bottom.mas_equalTo(-10);
        }];
        [self layoutIfNeeded];
        //重新排列位置
        self.pageControl.type = PageControlDirectionMiddle;
        DggWEAKSELF(weakSelf);
        
        self.cycleView.itemDidScrollOperationBlock = ^(NSInteger currentIndex) {
            
            weakSelf.pageControl.currentPage = currentIndex;
            if (weakSelf.scrollBlock) {
                weakSelf.scrollBlock(currentIndex);
            }
        };
    }
    return self;
}

- (void)configCellWithAdsArr:(NSArray *)adsArr duration:(CGFloat)duration isShowPageControl:(BOOL)isShowPageControl adsClick:(nullable void(^)(NSInteger index))adsClick scrollBlock:(nullable void (^)(NSInteger))scrollBlock {
    
        [self.cycleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
        }];
        
        self.cycleView.imageURLStringsGroup = adsArr ?: @[];
        self.pageControl.hidden = !isShowPageControl;
        self.pageControl.numberOfPages = self.cycleView.imageURLStringsGroup.count;
        self.cycleView.autoScrollTimeInterval = duration;
        self.cycleView.infiniteLoop = self.cycleView.imageURLStringsGroup.count  > 1;
        self.cycleView.autoScroll = self.cycleView.imageURLStringsGroup.count  > 1;
    
        self.adsClick = adsClick;
        self.scrollBlock = scrollBlock;
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    if (self.adsClick) {
        self.adsClick(index);
    }
}

- (SDCycleScrollView *)cycleView {
    
    if (_cycleView == nil) {
        _cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth , self.height) delegate:self placeholderImage:nil];
        _cycleView.layer.cornerRadius = 3;
        _cycleView.layer.masksToBounds = YES;
        _cycleView.showPageControl = NO;
        _cycleView.placeholderImage = [UIImage imageNamed:@"img_bannerzanwei"];
        _cycleView.backgroundColor = [UIColor clearColor];
        [self addSubview:_cycleView];
    }
    return _cycleView;
}

- (CustomPageControl *)pageControl {
    if (!_pageControl) {
        //CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 34.f)
        _pageControl = [[CustomPageControl alloc] initWithFrame:CGRectZero];
        _pageControl.currentColor = [UIColor colorWithHexString:@"#4974F5"];
        _pageControl.otherColor = [UIColor whiteColor];
        _pageControl.controlSize = CGSizeMake(6.f, 6.f);
        _pageControl.controlSpacing = 6;
        _pageControl.animationTime = 0.34;
        _pageControl.currentMultiple = 1;
//        _pageControl.type = PageControlDirectionRight;
    }
    return _pageControl;
}
@end
